package com.xposed.cdxposed

import android.os.Build
import com.xposed.cdxposed.hook.EmojiGameHook
import com.xposed.cdxposed.hook.StepHook
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodReplacement
import de.robv.android.xposed.XSharedPreferences
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage
import kotlin.properties.Delegates

/**
 * Created by cd on 2018/7/19.
 */

class XposedInit : IXposedHookLoadPackage {
    companion object {
        var xsp by Delegates.notNull<XSharedPreferences>()
    }

    init {
        xsp = XSharedPreferences(BuildConfig.APPLICATION_ID, "config")
        xsp.makeWorldReadable()
    }

    @Throws(Throwable::class)
    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
        when(lpparam.packageName) {
            "com.tencent.tmgp.sgame" -> {
                XposedHelpers.setStaticObjectField(Build::class.java, Constant.MANUFACTURER, "OPPO")
                XposedHelpers.setStaticObjectField(Build::class.java, Constant.BRAND, "OPPO")
                XposedHelpers.setStaticObjectField(Build::class.java, Constant.PRODUCT, "R11 Plus")
                XposedHelpers.setStaticObjectField(Build::class.java, Constant.DEVICE, "R11 Plus")
                XposedHelpers.setStaticObjectField(Build::class.java, Constant.MODEL, "OPPO R11 Plus")
            }
            "com.tencent.mm" -> {
                xsp.reload()
                StepHook.hook() //步数助手
                EmojiGameHook.hook(lpparam) //猜拳和投骰子助手
            }
            //Hook掉模块验证方法返回true，验证模块是否生效
            "com.xposed.cdxposed" -> {
                XposedHelpers.findAndHookMethod("com.xposed.cdxposed.ui.SettingActivity",
                        lpparam.classLoader, "isModuleActive", XC_MethodReplacement.returnConstant(true))
            }
        }
    }
}
